/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.swagger.transform;

import com.example.swagger.common.base.AbstractOfficeTransformPlugin;
import com.example.swagger.common.enums.FileTypeEnum;
import com.example.swagger.common.util.TransformHelper;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Paths;

/**
 * <p>
 * PDF 转化为 DOC
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-09 13:56
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-09
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class PdfAndDocTransformPlugin extends AbstractOfficeTransformPlugin {

    @Override
    public void gen() {
        gen(false);
    }

    @Override
    public void transform() {
        String source = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_DOC,FileTypeEnum.FILE_TYPE_PDF);
        String target = TransformHelper.getTransformFilePath(FileTypeEnum.FILE_TYPE_PDF,FileTypeEnum.FILE_TYPE_DOC,FileTypeEnum.FILE_TYPE_DOC);
        File docFile = new File(target);
        try {
            TransformHelper.initFile(docFile);
            PDDocument doc = PDDocument.load(Paths.get(source).toFile());
            int pages = doc.getNumberOfPages();
            FileOutputStream fos = new FileOutputStream(docFile);
            //文件输入流，输入文本文件
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(pages);
            stripper.writeText(doc, writer);
            writer.close();
            doc.close();
        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    @Override
    public void transformReversal() {
        // TODO word 转 pdf

    }
}
